package cj.web.cc.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import cj.utils.Constants;
import cj.utils.MD5Security;
import cj.web.cc.dao.MemberDAO;
import cj.web.constants.MemuEnum;
import cj.web.constants.SexEnum;
import cj.web.sc.dao.AppTokenDAO;
import cj.web.sc.dao.OrderDAO;
import cj.web.uc.dao.LoginDAO;

@Path("uc")
public class MemberController extends BaseController{
	@Autowired
	private MemberDAO memberDAO;
	@Autowired
	private AppTokenDAO appTokenDao;
	@Autowired
	private OrderDAO orderDao;
	@Autowired
	private LoginDAO loginDao;
	
	/**
	 * 修改密码界面
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upPwdPage")
	public String upPwdPage(Model model, HttpServletRequest request){
		if(isLogin(request)){
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			model.add("phone", phone);
		}
		model.add("currentTopMemu", MemuEnum.memugrzx);
		return "/views/uc/wx/upPwd.vm";
	}
	
	/**
	 * 修改密码界面-PC
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upPcPwdPage")
	public String upPcPwdPage(Model model, HttpServletRequest request){
		if(isLogin(request)){
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			model.add("phone", phone);
		}
		model.add("currentMemu", MemuEnum.xgmm);
		model.add("currentTopMemu", MemuEnum.memugrzx);
		return "/views/uc/upPcPwd.vm";
	}
	
	/**
	 * 修改密码
	 * @param phone
	 * @param newPwd
	 * @param oldPwd
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Post("upPwd")
	public String upPwd(@Param("memberId")int memberId, @Param("newPwd")String newPwd, 
			@Param("oldPwd")String oldPwd, HttpServletResponse response) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> uc = memberDAO.getMemberById(memberId);
		if(uc.size() == 0){
			//不存在
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_2);
		}else if(uc.size()>0){
			//存在
			String pwd = uc.get(0).get("pwd").toString();//pwd
			
			if(pwd.equals(MD5Security.md5(oldPwd))){
				memberDAO.updateMemberId(memberId, MD5Security.md5(newPwd));
			}else{
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			}
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 用户完善信息界面
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upInforPage")
	public String ucInforPage(Model model, HttpServletRequest request){
		model.add("sexObj",EnumSet.allOf(SexEnum.class));
		if(isLogin(request)){
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			List<Map> uc = memberDAO.getMemberById(Integer.parseInt(memberId));
			if(uc.size()>0){
				model.add("obj", uc.get(0));
			}else{
				model.add("obj", null);
			}
		}
		return "/views/uc/wx/updateInfor.vm";
	}
	
	/**
	 * 用户完善信息界面
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upPcInforPage")
	public String upPcInforPage(Model model, HttpServletRequest request){
		model.add("sexObj",EnumSet.allOf(SexEnum.class));
		if(isLogin(request)){
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			model.add("phone", phone);
			List<Map> uc = memberDAO.getMemberById(Integer.parseInt(memberId));
			if(uc.size()>0){
				model.add("obj", uc.get(0));
			}else{
				model.add("obj", null);
			}
		}
		model.add("currentMemu", MemuEnum.wszl);
		model.add("currentTopMemu", MemuEnum.memugrzx);
		return "/views/uc/updatePcInfor.vm";
	}
	
	/**
	 * 用户信息修改
	 * @param model
	 * @param memberId
	 * @param name
	 * @param sex
	 * @param age
	 * @param career
	 * @param request
	 * @return
	 */
	@Post("upInfor")
	public String ucInforPage(Model model,@Param("memberId")int memberId,@Param("name")String name,@Param("sex")String sex,
			@Param("age")String age,@Param("career")String career, HttpServletRequest request,HttpServletResponse response){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> uc = memberDAO.getMemberById(memberId);
		if(uc.size() == 0){
			//不存在
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}else if(uc.size()>0){
			//存在
			setCookie(response, "sex", sex);//性别
			memberDAO.updateMemberId(memberId, name, age, sex, career);
		}
		return "@" + jo.toString();
	}
}